#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

//    host_server=new QTcpServer(this);
//    clientSocket =nullptr;
//    connect(host_server,&QTcpServer::newConnection,this,&MainWindow::onNewConnection);
//    if(! host_server->listen(QHostAddress::Any,8080))
//    {
//        qDebug() << "Server could not start!";
//    }
//    else
//    {
//        qDebug() << "Server started on 8080";
//    }


}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::onNewConnection()
{
    clientSocket =host_server->nextPendingConnection();
    connect(clientSocket ,&QTcpSocket::readyRead,this,&MainWindow::read_date);
}

void MainWindow::read_date()
{
    qDebug()<<"~~~~~~~~~~";
    // 获取客户端发来的数据
    QJsonDocument jsonResponse = QJsonDocument::fromJson(clientSocket ->readAll());
    QJsonObject jsonObject = jsonResponse.object();

    qDebug() << "request is: ";
    qDebug().noquote() << QJsonDocument(jsonResponse).toJson(QJsonDocument::Indented);

    // 获取客户端发来的数据中的相关字段
    double fly_height = jsonObject["height"].toDouble();
    QJsonArray overlapArray = jsonObject["overlap"].toArray();
    double overlap = overlapArray.at(1).toDouble()/100;
    std::cout << "Fly height: " << fly_height << ", Overlap: " << overlap << std::endl;
    double image_focal = 3000;
    double image_width = 4000;
    double gap = get_gap_from_camera_overlap(image_focal,image_width,fly_height,overlap);

    QJsonArray coordinatesArray = jsonObject["poly"].toObject()["geometry"].toObject()["coordinates"].toArray();
    std::vector<GSLAM::Point3d> poly;
    for(int i=0;i<coordinatesArray.size();i++)
    {
        QJsonArray pointArray = coordinatesArray[i].toArray();

        double x = pointArray.at(0).toDouble();
        double y = pointArray.at(1).toDouble();
        poly.push_back(GSLAM::Point3d(x, y, fly_height));
    }

    QJsonArray landingPointArray = jsonObject["landing_point"].toArray();
    double angle = jsonObject["angle"].toDouble();
    double landingPointX = landingPointArray.at(0).toDouble();
    double landingPointY = landingPointArray.at(1).toDouble();
    GSLAM::Point3d landingPoint(landingPointX, landingPointY, fly_height);
    poly = ploy_to_path(poly,landingPoint,gap,angle);

    QJsonObject responseData;
    responseData["sno"] = 0;
    responseData["result"] = 0;
    QJsonObject dataObject;
    QJsonArray coordinatesArray_response;
    for(int i=0; i<poly.size(); i++)
    {
        QJsonArray temp;
        temp.append(poly[i].x);
        temp.append(poly[i].y);
        temp.append(poly[i].z);
        coordinatesArray_response.append(temp);
    }
    dataObject["coordinates"] = coordinatesArray_response;
    dataObject["type"] = "LineString";
    responseData["data"] = dataObject;
    responseData["message"] = "Success";

    // 发送响应数据给客户端
    QJsonDocument response(responseData);
    clientSocket->write(response.toJson());
    clientSocket->waitForBytesWritten();
//    clientSocket->disconnectFromHost();
//    clientSocket->waitForDisconnected();



}

